
package scalaExec.gui;

import scalaExec.Interpreter.GlobalValues;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

// this frame displays the Autocompletion results and allows to get detailed help
// by double clicked on a value
public class AutoCompletionFrame extends JFrame   {
  JFrame thisFrame;    
    /** Creates a new instance of AutoCompletionFrame */
  public AutoCompletionFrame(String title) {
        super(title);
        thisFrame = this;
    }
    
  // display the matches of the autocompletion search passed with the resultsList parameter
  public  void displayMatches( JList resultsList )
     {
        JPanel  helpPanel = new JPanel();
        JScrollPane  listScroll = new JScrollPane(resultsList);
           
        resultsList.addKeyListener(new KeyListener() {

            @Override
            public void keyTyped(KeyEvent evt) {
        int keyValue = evt.getKeyCode();
        
        switch (keyValue) {
                              
            case   KeyEvent.VK_ENTER:  
             }
            }

            @Override
            public void keyPressed(KeyEvent evt) {
                       int keyValue = evt.getKeyCode();
        
        switch (keyValue) {
                              
            case   KeyEvent.VK_ENTER:  
             JList source = (JList) evt.getSource();   // get the list in which this result belongs
             Object  selection = source.getSelectedValue();  
             String selValue = (String)selection;
             scalaExec.Interpreter.GlobalValues.userConsole.textArea += selValue;  // reset the text from the current command
             thisFrame.dispose();
             GlobalValues.autoCompletionVariableSelected = true;
  
            default:
             
        
        }
//                throw new UnsupportedOperationException("Not supported yet.");
            }

            @Override
            public void keyReleased(KeyEvent e) {
  //              throw new UnsupportedOperationException("Not supported yet.");
            }
        });
        
     
      
        resultsList.addKeyListener(new autoCompleteListHandler());
        
        resultsList.addMouseListener(new MouseAdapter() {
        public void mouseClicked(MouseEvent evt) {
         if (evt.getClickCount() == 2) {   // double click on an autocompletion result
                     
             JList source = (JList) evt.getSource();   // get the list in which this result belongs
             Object []  selection = source.getSelectedValues();  
             String selValue = (String)selection[0];
             ConsoleKeyHandler.display_detailed_help(selValue);   // get detailed help
         }
        }
        });
        helpPanel.setLayout(new BorderLayout());             
        helpPanel.add(listScroll);
        add(helpPanel);
        setSize(GlobalValues.scalalabMainFrame.getSize().width/4, GlobalValues.scalalabMainFrame.getSize().height/4);
        int xp = 100; int yp = 200; 
        if (GlobalValues.myEdit != null)  {    // if ScalaLab editor has been opened  use mouse coordinates
          Point editorLoc = GlobalValues.myEdit.textArea.getMousePosition();
          if (editorLoc != null) {
             xp = editorLoc.x;  yp = editorLoc.y;
            }
          }
        else {  // use ScalaLab Main Frame
            Point mainFrameLoc = GlobalValues.scalalabMainFrame.getMousePosition();
            if (mainFrameLoc != null)  {
             xp = mainFrameLoc.x;  yp = mainFrameLoc.y;
            }
            else { xp = 400; yp  =400; }
        }
        setLocation(xp, yp);
        
        setVisible(true);
    }
    
    
    public void windowActivated(WindowEvent e) {
   // JOptionPane.showMessageDialog(null, "ok");
    }

    
        
}
    
    

